package com.hx.mypetstore.ui.main;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.hx.mypetstore.Bean.StoreBean;
import com.hx.mypetstore.R;
import com.hx.mypetstore.ui.goods.GoodsActivity;
import com.hx.mypetstore.ui.petvideo.PetVideoAct;
import com.hx.mypetstore.ui.push.PushConst;
import com.hx.mypetstore.ui.push.PushService;
import com.hx.mypetstore.util.LoginUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;


public class mainFragment extends Fragment{
    /**
     * 轮播
     */
    private Banner banner;
    /**
     * 定义一个自己的context
     */
    private Context mContext;
    private ArrayList<Integer> imageList = new ArrayList<>();
    private StoreBean storeBean;
    private TextView tx_name;
    private TextView tx_distance;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_main,container,false);
     //        初始化控件
        initView(view);
        initDate();
        return view;
    }

    private String address;

    private Integer longitude;

    private Integer dimension;
    private void initDate() {
        storeBean = new StoreBean("憨憨鹏的宠物店","厦门市集美区天马路",118.094088,24.638329);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initView(View view) {
        mContext = getContext();
        banner = view.findViewById(R.id.banner);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        initBanner();
        tx_name = view.findViewById(R.id.main_store_name);
        tx_distance = view.findViewById(R.id.main_store_distance);
        tx_distance.setText("距离：600米");
        //设置宠物商店监听器
        view.findViewById(R.id.main_petStore).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 宠物商店
                if (LoginUtil.isLogin(mContext)) {
                    // subscribe petstore topic
                    PushService.subscribe(mContext, PushConst.TOPIC_STORE);
                }
                if (LoginUtil.loginCheck(mContext)) {
                    startActivity(new Intent(mContext, GoodsActivity.class));
                }
            }
        });
        view.findViewById(R.id.main_petVideo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 宠物视频
                if (LoginUtil.isLogin(mContext)) {
                    // subscribe petvedio topic
                    PushService.subscribe(mContext, PushConst.TOPIC_VEDIO);
                }
                if (LoginUtil.loginCheck(mContext)) {
                    startActivity(new Intent(mContext, PetVideoAct.class));
                }
            }
        });
        view.findViewById(R.id.main_store_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),MapActivity.class));
            }
        });
    }

    /**
     * 初始化广告页
     */
    private void initBanner() {

        //清空旧数据
        imageList.clear();
        imageList.add(R.drawable.guanggao1);
        imageList.add(R.drawable.guanggao2);
        imageList.add(R.drawable.guanggao3);
        banner.setImages(imageList);
        banner.start();
    }

}
